/**
 * Created by yfyuan on 2016/10/28.
 */
'use strict';
cBoard.service('chartRadarService', function () {

    this.render = function (containerDom, option, scope, persist) {
        return new CBoardEChartRender(containerDom, option).chart(null, persist);
    };

    this.parseOption = function (_data) {
        var casted_keys = _data.keys;
        var casted_values = _data.series;
        var aggregate_data = _data.data;
        var newValuesConfig = _data.seriesConfig;

        var string_keys = _.map(casted_keys, function (key) {
            return key.join('-');
        });
        var string_values = _.map(casted_values, function (value) {
            return value.join('-');
        });
        var data = [];
        var max;
        for (var i = 0; i < string_values.length; i++) {
            var d = {value: [], name: string_values[i]};
            for (var j = 0; j < string_keys.length; j++) {
                d.value[j] = aggregate_data[i][j];
                var n = Number(aggregate_data[i][j]);
                if (_.isUndefined(max) || n > max) {
                    max = n;
                }
            }
            data.push(d);
        }
        var indicator = [];
        for (var j = 0; j < string_keys.length; j++) {
            indicator.push({name: string_keys[j], max: max * 1.05});
        }
        var echartOption = {
            tooltip: {
                trigger: 'item'
            },
            toolbox: false,
            legend: {
                orient: 'vertical',
                left: 'left',
                data: string_values
            },
            radar: {
                indicator: indicator
            },
            series: [{
                name: 'radar',
                type: 'radar',
                itemStyle: {
                    emphasis: {
                        areaStyle: {color: 'rgba(0,250,0,0.3)'}
                    }
                },
                data: data
            }]
        };
        return echartOption;
    };
});